;



$(function () {
    async function show() {
        var result = await $.get("http://127.0.0.1:8080/api/getCarts", {
            uId: JSON.parse($.cookie("yolence")).uid
        });
        console.log(result)
        // cid: 9
        // pImg: "/images/boluo1-small.jpg"
        // pName: "云南西双版纳手撕金钻凤梨"
        // pNum: 1
        // pPrice: "59.9"
        // pTotal: "59.9"
        // pid: 2
        // uid: 1

        var xznum = $(this).parent().next().next().find("input").text();
        var strHtml = ``;
        result.forEach(ele => {
            strHtml += `<tr>
            <td class="ttd" style="width:90px;padding-left: 18px; display: inline-block">
                <input id="nb" class="nb" type="checkbox" data-pid=${ele.pid}><img src="http://127.0.0.1:8080/${ele.pImg}"
                    alt="">
            </td>
            <td style="width: 500px;text-indent: 12px;display: inline-block; ">${ele.pName}</td>
            <td style="width: 166px;text-indent: 70px;display: inline-block;">
                <div class="num">
                <a class="lessa" href="#"></a>
                <input id="numBuy" class="numBuy" type="text" value="${ele.pNum}">
                <a class="morea" href="#"></a>
                </div>
            </td>

            <td id="cash" class="cash" style="width: 175px;padding-right: 15px;text-align: right;display: inline-block;">${ele.pPrice}</td>
            <td id="sum" class="sum" style="width: 150px;text-align:center;display: inline-block;">${ele.pTotal}</td>
            <td style="width: 90px;display: inline-block;"><a class="del" style="color:red" href="javascript:void(0)">删除</a></td>
        </tr>`

        });
        $("#goods-tb").append(strHtml);








        //数量加法
        $("body").on("click", ".morea", function () {

            var n = $(this).prev().val();
            var num = parseInt(n) + 1;
            if (num == 0) {
                return;
            }
            $(this).prev().val(num);
            //console.log(num) 查看数量
            function setprice() {
                var chushiint = 0;
                var xznums = 0;
                var xzzhongs=0;
                $(".nb").each(function () {
                    if ($(this).is(":checked")) {
                        var xzprice = parseInt($(this).parent().next().next().next().next().text());
                        var xznum = parseInt($(this).parent().next().next().find(".numBuy").val());
                        xzzhongs++;
                        //console.log(xzprice)
                        xznums += xznum;
                        chushiint += xzprice;
                    }
                });
                $(".kind").text(xzzhongs);
                $(".money").text(chushiint);
                $(".toTal").text(xznums);

            };
            $(this).parent().parent().next().next().text($(this).parent().parent().next().text() * $(this).prev().val())
            setprice();
            // $("tr>td:eq(4)").text($("tr>td:eq(3)").text()*num)
            var saveObj = {
                uId: JSON.parse($.cookie("yolence") || "{}").uid,
                pId: $(this).parents("tr").find("#nb").data("pid"),
                pNum: num
            }
            console.log(saveObj)

            $.ajax({
                url: "http://127.0.0.1:8080/api/modify",
                data: saveObj,
                type: "post",
                dataType: "json",
            }).then((data) => {
                if (data.affectedRows >= 1) {
                    layer.msg("修改成功哦", {
                        icon: 1
                    });
                } else {
                    layer.msg("修改失败哦", {
                        icon: 2
                    });
                }
            })

        });

        

        //数量减法
        $("body").on("click", ".lessa", function () {
            var n = $(this).next().val();
            var num = parseInt(n) - 1;
            if (num == 0) {
                return
            }
            $(this).next().val(num);

            function setprice() {
                var chushiint = 0;
                var xznums = 0;
                var xzzhongs=0
                $(".nb").each(function () {
                    if ($(this).is(":checked")) {
                        var xzprice = parseInt($(this).parent().next().next().next().next().text());
                        var xznum = parseInt($(this).parent().next().next().find(".numBuy").val());
                        xznums += xznum;
                        chushiint += xzprice;
                        xzzhongs++;
                    }
                });
                $(".money").text(chushiint);
                $(".toTal").text(xznums);
                $(".kind").text(xzzhongs);
            };
            $(this).parent().parent().next().next().text($(this).parent().parent().next().text() * $(this).next().val())



            setprice();
            var saveObj = {
                uId: JSON.parse($.cookie("yolence") || "{}").uid,
                pId: $(this).parents("tr").find("#nb").data("pid"),
                pNum: num
            }
            //console.log(saveObj)

            $.ajax({
                url: "http://127.0.0.1:8080/api/modify",
                data: saveObj,
                type: "post",
                dataType: "json",
            }).then((data) => {
                if (data.affectedRows >= 1) {
                    layer.msg("修改成功哦", {
                        icon: 1
                    });
                } else {
                    layer.msg("修改失败哦", {
                        icon: 2
                    });
                }
            })


        });
        $("body").on("click", ".del", function () {
            var deleteObj = {
                uId: JSON.parse($.cookie("yolence") || "{}").uid,
                pId: $(this).parents("tr").find("#nb").data("pid"),
            }
            layer.confirm("你可看清楚了啊,这是删除你刚刚精挑细选的宝物啊!", {
                    btn: ["不爱了,不要", "那我再好好想想吧..."]
                },
                (index) => {
                    layer.close(index);
                    $(this).parents("tr").remove();
                    $.ajax({
                        url: "http://127.0.0.1:8080/api/delete",
                        type: "post",
                        data: deleteObj
                    }).done((data) => {
                        console.log(data)
                    })
                },
                () => {

                }
            )

        })


        $("body").on("click", "#select", function () {
            if ($(this).is(":checked")) {
                $(".nb").prop("checked", true)
            } else {
                $(".nb").prop("checked", false)
            }
        })



        $("body").on("click", ".nb", function () {
            if ($(this).is(":checked")) {
                $(this).attr("checked", true);
                var checked_length = $("input[class='nb']:checked").length;
                var nb_length = $(".nb").length;
                if (nb_length == checked_length) { //选框个数=选中选框个数
                    $("#select").prop("checked", true); //勾选全选
                    setprice();
                }
                setprice()
            } else { //没选中
                $(this).attr("checked", false); //当前check属性消除
                $("#select").prop("checked", false); //取消全选
                setprice();
            };

            function setprice() {
                var chushiint = 0;
                $(".nb").each(function () {
                    if ($(this).is(":checked")) {
                        var xzprice = parseInt($(this).parent().next().next().next().next().text());
                        console.log(xzprice)
                        chushiint += xzprice;
                    }
                });
                $(".money").text(chushiint);


            };
            setprice();

        })







    }

    show();


})